Remapping Keys and Buttons


Introduction

The feature described on this page is not supported on Windows 95/98/Me or for joysticks. See the following alternate methods for them:

Limitation: AutoHotkey's remapping feature described below is generally not as pure and effective as remapping directly via the Windows registry. For the advantages and disadvantages of each approach, see registry remapping.

Remapping the Keyboard and Mouse [v1.0.40+]

The syntax for the built-in remapping feature is OriginKey::DestinationKey. For example, a script consisting only of the following line would make the "a" key behave like the "b" key:

a::b

The above example does not alter the "b" key itself. The "b" key would continue to send the "b" keystroke unless you remap it to something else as shown in the following example:

a::b
b::a

The examples above use lowercase, which is recommended for most purposes because it also remaps the corresponding uppercase letters (that is, it will send uppercase when Capslock is "on" or the Shift key is held down). By contrast, specifying an uppercase letter on the right side forces uppercase. For example, the following line would produce an uppercase B when you type either "a" or "A" (as long as Capslock is off):

a::B

 

Mouse remapping: To remap the mouse instead of the keyboard, use the same approach. For example:

MButton::Shift Makes the middle button behave like the Shift key.
XButton1::LButton Makes the fourth mouse button behave like the left mouse button.
RAlt::RButton Makes the right Alt key behave like the right mouse button.

 

Other useful remappings:

Capslock::Ctrl Makes Capslock become a Control key.
XButton2::^LButton Makes the fifth mouse button (XButton2) produce Control-LeftClick.
RAlt::AppsKey Makes the right Alt key become the Apps key (which is the key that opens the context menu).
RCtrl::RWin Makes the right Control key become the right Windows key.
Ctrl::Alt Makes both Control keys behave like an Alt key. However, see alt-tab issues.
^x::^c Makes Control-X produce Control-C. It also makes Control-Alt-X produce Control-Alt-C, etc.
RWin::Return Disables the right Windows key by having it simply return.

You can try out any of these examples by copying them into a new text file such as "Remap.ahk", then launching the file.

See the Key List for a complete list of key and mouse button names.

Remarks

The directives #IfWinActive/Exist can be used to make selected remappings active only in the windows you specify. For example:

#IfWinActive ahk_class Notepad
a::b  ; Makes the 'a' key send a 'b' key, but only in Notepad.
#IfWinActive  ; This puts subsequent remappings and hotkeys in effect for all windows.

Remapping a key or button is "complete" in the following respects:

Although a remapped key can trigger normal hotkeys, it cannot trigger mouse hotkeys or hook hotkeys (use ListHotkeys to discover which hotkeys are "hook"). For example, if the remapping a::b is in effect, pressing Ctrl-Alt-A would trigger the ^!b hotkey only if ^!b is not a hook hotkey. If ^!b is a hook hotkey, you can define ^!a as a hotkey if you want Ctrl-Alt-A to perform the same action as Ctrl-Alt-B. For example:

a::b
^!a::
^!b::
ToolTip You pressed %A_ThisHotkey%.
return

If SendMode is used in the auto-execute section (top part of the script), it affects all remappings. However, since remapping uses Send {Blind} and since the SendPlay mode does not fully support {Blind}, some remappings might not function properly in SendPlay mode (especially Control, Shift, Alt, and Win). To work around this, avoid SendPlay in auto-execute section when you have remappings; then use the command SendPlay vs. Send in other places throughout the script. Alternatively, you could translate your remappings into hotkeys (as described below) that explicitly call SendEvent vs. Send.

When a script is launched, each remapping is translated into a pair of hotkeys. For example, a script containing a::b actually contains the following two hotkeys instead:

*a::
SetKeyDelay -1   ; If the destination key is a mouse button, SetMouseDelay is used instead.
Send {Blind}{b DownTemp}  ; DownTemp is like Down except that other Send commands in the script won't assume "b" should stay down during their Send.
return

*a up::
SetKeyDelay -1  ; See note below for why press-duration is not specified with either of these SetKeyDelays.
Send {Blind}{b Up}
return

However, the above hotkeys vary under the following circumstances:

  1. When the source key is LCtrl and the destination key is an Alt key, the line Send {Blind}{LAlt DownTemp} is replaced by Send {Blind}{LCtrl Up}{LAlt DownTemp}. The same is true if the source is RCtrl, except that {RCtrl up} is used.
  2. When a keyboard key is being remapped to become a mouse button (e.g. RCtrl::RButton), the hotkeys above use SetMouseDelay in place of SetKeyDelay. In addition, the first hotkey above is replaced by the following, which prevents the keyboard's auto-repeat feature from generating repeated mouse clicks:
    *RCtrl::
    SetMouseDelay -1
    if not GetKeyState("RButton")  ; i.e. the right mouse button isn't down yet.
        Send {Blind}{RButton DownTemp}
    return

Note that SetKeyDelay's second parameter (press duration) is omitted in the hotkeys above. This is because press-duration does not apply to down-only or up-only events such as {b down} and {b up}. However, it does apply to changes in the state of the Shift/Ctrl/Alt/Win keys, which affects remappings such as a::B or a::^b. Consequently, any press-duration a script puts into effect via its auto-execute section will apply to all such remappings.

Since remappings are translated into hotkeys as described above, the Suspend command affects them. Similarly, the Hotkey command can disable or modify a remapping. For example, the following two commands would disable the remapping a::b.

Hotkey, *a, off
Hotkey, *a up, off

Alt-tab issues: If you remap a key or mouse button to become an Alt key, that key will probably not be able to alt-tab properly. A possible work-around is to add the hotkey *Tab::Send {Blind}{Tab} -- but be aware that it will likely interfere with using the real Alt key to alt-tab. Therefore, it should be used only when you alt-tab solely by means of remapped keys and/or alt-tab hotkeys.

In addition to the keys and mouse buttons on the Key List page, the source key may also be a virtual key (VKnn) or scan code (SCnnn) as described on the special keys page. The same is true for the destination key except that it may optionally specify a scan code after the virtual key. For example, sc01e::vk42sc030 is equivalent to a::b on most keyboard layouts.

To disable a key rather than remapping it, make it a hotkey that simply returns. For example, F1::return would disable the F1 key.

The following keys are not supported by the built-in remapping method:

Moving the Mouse Cursor via the Keyboard

The keyboard can be used to move the mouse cursor as demonstrated by the fully-featured Keyboard-To-Mouse script. Since that script offers smooth cursor movement, acceleration, and other features, it is the recommended approach if you plan to do a lot of mousing with the keyboard. By contrast, the following example is a simpler demonstration:

*#up::MouseMove, 0, -10, 0, R  ; Win+UpArrow hotkey => Move cursor upward
*#Down::MouseMove, 0, 10, 0, R  ; Win+DownArrow => Move cursor downward
*#Left::MouseMove, -10, 0, 0, R  ; Win+LeftArrow => Move cursor to the left
*#Right::MouseMove, 10, 0, 0, R  ; Win+RightArrow => Move cursor to the right

*<#RCtrl::  ; LeftWin + RightControl => Left-click (hold down Control/Shift to Control-Click or Shift-Click).
SendEvent {Blind}{LButton down}
KeyWait RCtrl  ; Prevents keyboard auto-repeat from repeating the mouse click.
SendEvent {Blind}{LButton up}
return

*<#AppsKey::  ; LeftWin + AppsKey => Right-click
SendEvent {Blind}{RButton down}
KeyWait AppsKey  ; Prevents keyboard auto-repeat from repeating the mouse click.
SendEvent {Blind}{RButton up}
return

Remapping via the Registry's "Scancode Map"

Advantages:

Disadvantages:

How to Apply Changes to the Registry: There are at least two methods to remap keys via the registry:

  1. Use a program like KeyTweak (freeware) to visually remap your keys. It will change the registry for you.
  2. Remap keys manually by creating a .reg file (plain text) and loading it into the registry. This is demonstrated at http://www.autohotkey.com/forum/post-56216.html#56216

Remapping Method for Windows 95/98/Me

The recommended method is to use Send and KeyWait. For example, the following hotkey makes the 'A' key become the left-arrow key:

a::
Send {Left down}  ; Hold down the left-arrow key.
KeyWait a  ; Wait for the user to release the key.
Send {Left up}  ; Release the left-arrow key.
return

Related Topics

List of keys and mouse buttons
GetKeyState
Remapping a joystick